package com.shanghaionstar.carmanage.presenter;

import android.content.Context;

import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.enumtype.PackageType;
import com.onstar.cn.more.entity.PackageInfo;
import com.onstar.cn.more.response.DataPackageRule;
import com.shanghaionstar.carmanage.iview.ICarFlowPackageView;
import com.shanghaionstar.carmanage.service.FlowPackageService;

import java.util.List;

import rx.functions.Action1;

/**
 * Created by Yonglang Hong on 2016/7/26.
 */
public class CarFlowPackagePresenter extends BasePresenter{

    FlowPackageService packageService;

    ICarFlowPackageView packageView;

    public void setPackageView(ICarFlowPackageView packageView) {
        this.packageView = packageView;
    }

    public CarFlowPackagePresenter(IMVPScheduler mvpScheduler, Context context) {
        super(mvpScheduler);
        packageService = new FlowPackageService(context);
    }

    public void getFlowPackage(final PackageType packageTypes){
        subscription = packageService.getFlowPackage(packageTypes)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<List<PackageInfo>>() {
                    @Override
                    public void call(List<PackageInfo> packageInfos) {
                        packageView.showPackageInfos(packageInfos);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        packageView.showErronLoadPackageInfos(throwable);
                    }
                });
    }

    public void getFlowPackageRule(){
        subscription = packageService.getflowPackageRule()
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<DataPackageRule>() {
                    @Override
                    public void call(DataPackageRule packageRule) {
                        packageView.showPackageRule(packageRule);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        packageView.showErrOnLoadPackageRule(throwable);
                    }
                });
    }
}
